package com.zhugang.week07;

/**
 * @program algorithms
 * @description: convertBiNode
 * @author: chanzhugang
 * @create: 2022/07/27 15:50
 */
public class ConvertBiNode {

    /**
     * 面试题 17.12. BiNode
     *
     * @param root
     * @return
     */

    TreeNode dummyHead = new TreeNode();
    TreeNode tail = dummyHead;

    public TreeNode convertBiNode(TreeNode root) {
        // 二叉搜索树按中序遍历
        dfs(root);
        return dummyHead.right;
    }

    private void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.left);
        // 中序遍历逻辑
        tail.right = root;
        tail = root;
        root.left = null;
        dfs(root.right);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int x) {
            val = x;
        }
    }
}